package cn.edu.cug.cs.gtl.docsrv.shp.block;

import cn.edu.cug.cs.gtl.docsrv.shp.querier.DefaultFeatureQuerier;
import cn.edu.cug.cs.gtl.docsrv.shp.querier.DefaultGeometryQuerier;
import cn.edu.cug.cs.gtl.docsrv.shp.querier.FeatureQuerier;
import cn.edu.cug.cs.gtl.docsrv.shp.querier.GeometryQuerier;
import cn.edu.cug.cs.gtl.extractor.ShapeExtractor;
import cn.edu.cug.cs.gtl.mybatis.mapper.shp.ShpAll;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;

import java.util.ArrayList;
import java.util.List;

/**
 * 图形单元块，由三个文件构成，SHP、DBF、SHX
 * ShpManager管理的基本单元就是ShapeBlock
 */
public class ShapeBlock  {
    ShpAll shpAll;
    List<SimpleFeature>  features;
    Envelope envelope;

    /**
     * 通过传入的ShpAll初始化
     * @param shpAll
     */
    public ShapeBlock (ShpAll shpAll){
        this.shpAll=shpAll;
        try {
            this.features= ShapeExtractor.parseToFeatures(shpAll.getShpRawBytes());
            this.envelope = new Envelope();
            for(SimpleFeature sf : this.features){
                this.envelope.expandToInclude(
                        ((Geometry)(sf.getDefaultGeometry())).getEnvelopeInternal());
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取Block中的所有要素
     * @return
     */
    public List<SimpleFeature> getFeatures() {
        return this.features;
    }

    /**
     * 获取要素查询器
     * @return
     */
    public FeatureQuerier  getFeatureQuerier(){
        return new DefaultFeatureQuerier(this);
    }


    public ShpAll getShpAll(){
        return this.shpAll;
    }
}
